//
//  ConsultationCollectionView.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/1/31.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

private let itemSize: CGFloat = 199
var models :[GoodsModel] = []

class ConsultationCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource {
    let banner = ["subBanner1.jpg", "subBanner2", "subBanner3.jpg", "subBanner4.jpg", "subBanner5.jpg"]
    weak var delegateVc: UIViewController!
    
    var searcModels: [SearchRecommendModel] = []
    init() {
        super.init(frame: .zero, collectionViewLayout: UICollectionViewLayout())
        setUI() 
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        setUI()
    }
    
    func setUI() {
        self.showsHorizontalScrollIndicator = false
        self.delegate = self
        self.dataSource = self
        self.register(UINib(nibName: "\(ConsultationCollectionViewCell.self)", bundle: nil), forCellWithReuseIdentifier: "cell")
        self.backgroundColor = ONColor.cellBGColor
    }
    
    func setData(arr:[GoodsModel]) {
        models = arr
        self.layoutIfNeeded()
        let followLayout = UICollectionViewFlowLayout()
        followLayout.minimumInteritemSpacing = 20
        followLayout.itemSize = CGSize(width: itemSize, height: self.height - 10)
        followLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
        followLayout.scrollDirection = .horizontal
        self.contentOffset = CGPoint.init(x: 1, y: 1)
        self.collectionViewLayout = followLayout
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return models.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? ConsultationCollectionViewCell
//        cell?.showImageView.image = UIImage(named: [indexPath.row])
        
        let model = models[indexPath.row]
        if let coverpath = model.coverPath{
            let path:String = "\(BaseURL)\(coverpath)"
            let url = NSURL.init(string: path)
            cell?.showImageView.sd_setImage(with: url! as URL, completed: nil)
        }
        if let name: String = model.goodsName {
            cell?.titleLabel.text = name
        }
        if let type: String = model.label?.name {
            cell?.typeLabel.setTitle(type, for: UIControlState.normal)
        }
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)

        let model1 = models[indexPath.row]
        var model: BannerModel = BannerModel()
        model.id = model1.id
        model.type = model1.type
        model.url = model1.contentUrl
        model.coverPath = model1.coverPath
        self.delegateVc.bannerJump(model: model)
      
    }
    
}
